<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>File Upload</title>
</head>
<body>
    <h1>Upload Data File</h1>
    <form id="uploadForm" enctype="multipart/form-data">
        <input type="file" id="fileInput" name="file" accept=".csv,.json,.xlsx" required>
        <button type="submit">Upload</button>
    </form>
    <div id="status"></div>

    <script>
        document.getElementById('uploadForm').addEventListener('submit', async (e) => {
            e.preventDefault();
            const fileInput = document.getElementById('fileInput');
            const file = fileInput.files[0];
            const statusDiv = document.getElementById('status');

            if (!file) {
                statusDiv.textContent = 'Please select a file.';
                return;
            }

            const formData = new FormData();
            formData.append('file', file);

            try {
                statusDiv.textContent = 'Uploading...';
                const response = await fetch('/upload', {
                    method: 'POST',
                    body: formData
                });

                const result = await response.json();
                if (response.ok) {
                    statusDiv.textContent = `Upload successful: ${result.filename}`;
                } else {
                    statusDiv.textContent = `Upload failed: ${result.error}`;
                }
            } catch (error) {
                statusDiv.textContent = `Error: ${error.message}`;
            }
        });
    </script>
</body>
</html>